using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using UwinEducation.API.Attributes;
using UwinEducation.Application.Common.Interfaces;
using UwinEducation.Application.Dtos;
using UwinEducation.Shared.Models;

namespace UwinEducation.API.Controllers
{
    /// <summary>
    /// 系统配置管理控制器
    /// </summary>
    [Authorize]
    //[RequirePermission("system.config")] // 指定所需的权限
    [ApiController]
    [Route("api/[controller]/[action]")]
    public class SystemConfigController : ControllerBase
    {
        private readonly ISystemConfigService _systemConfigService;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="systemConfigService">系统配置服务</param>
        public SystemConfigController(ISystemConfigService systemConfigService)
        {
            _systemConfigService = systemConfigService;
        }

        /// <summary>
        /// 获取所有系统配置
        /// </summary>
        /// <returns>系统配置列表</returns>
        [HttpPost]
        public async Task<ApiResponse<IEnumerable<SystemConfigDto>>> GetAll()
        {
            var configs = await _systemConfigService.GetAllAsync();
            return ApiResponse<IEnumerable<SystemConfigDto>>.Ok(configs);
        }

        /// <summary>
        /// 根据键获取系统配置
        /// </summary>
        /// <param name="request">键请求参数</param>
        /// <returns>系统配置</returns>
        [HttpPost]
        public async Task<ApiResponse<SystemConfigDto>> GetByKey([FromBody] KeyRequest request)
        {
            var config = await _systemConfigService.GetByKeyAsync(request.Key);
            if (config == null)
            {
                return ApiResponse<SystemConfigDto>.Error("配置不存在");
            }
            return ApiResponse<SystemConfigDto>.Ok(config);
        }

        /// <summary>
        /// 根据键获取系统配置值
        /// </summary>
        /// <param name="request">键请求参数</param>
        /// <returns>系统配置值</returns>
        [HttpPost]
        public async Task<ApiResponse<string>> GetValueByKey([FromBody] KeyRequest request)
        {
            var value = await _systemConfigService.GetValueByKeyAsync(request.Key);
            if (string.IsNullOrWhiteSpace(value))
            {
                return ApiResponse<string>.Error("配置不存在");
            }
            return ApiResponse<string>.Ok(value);
        }

        /// <summary>
        /// 更新系统配置
        /// </summary>
        /// <param name="input">更新系统配置请求参数</param>
        /// <returns>更新后的系统配置</returns>
        [HttpPost]
        [RequirePermission("system.config")] // 指定所需的权限
        public async Task<ApiResponse<SystemConfigDto>> Update([FromBody] UpdateSystemConfigDto input)
        {
            var config = await _systemConfigService.UpdateAsync(input);
            return ApiResponse<SystemConfigDto>.Ok(config);
        }
    }

    /// <summary>
    /// 键请求参数
    /// </summary>
    public class KeyRequest
    {
        /// <summary>
        /// 配置键
        /// </summary>
        public string Key { get; set; }
    }
}